/*
 *    This file is part of CeluMap.
 *
 *    CeluMap is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU Lesser General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    CeluMap is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    Copyright 2006, 2007, 2008 Gonzalo Merayo
 *    gonzalo (at) celumap.com.ar
 *    www.celumap.com.ar
 *
 */

package main;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import config.Properties;

import screen.ScreenGraphics;
import service.ServicesMap;
import map.MemMap;
import map.file.CombinationMap;
import map.file.FileMap;
import map.file.layer.LayerMap;
import mapDrawer.DefaultMapDrawer;

import figures.Figure;
import figures.FigureIterator;
import figures.images.WebIcon;
import file.HomeGetter;
import file.ResourceGetter;
import form.StreetFinder;
import geometry.MovingTransform;
import geometry.Rectangle;

public class Gis extends MIDlet implements CommandListener{
	
	private MapCanvas canvas =null;
	private Display display=null;
	private StreetFinder streetFinder = null;
	private MovingTransform transform= new MovingTransform();
	private DefaultMapDrawer drawer = null;
	private CombinationMap map = null;
	private ServicesMap services = null;
	
	
	protected void startApp() throws MIDletStateChangeException {
		display = Display.getDisplay(this);
		int splashtime = Integer.parseInt(Properties.getProperty("SPLASHTIME"));
		long time = System.currentTimeMillis();
		if(splashtime > 0)
			loadSplash(display);
		try{
			//Prepare Map
			map = (CombinationMap)LayerMap.loadLayers(ResourceGetter.getInstance(),Properties.getProperty("mapfile"), transform);
			drawer  = new DefaultMapDrawer(map);
			canvas = new MapCanvas(drawer, transform);
			transform.adjust(map.getArea(), canvas.getWidth(), canvas.getHeight());
			//Create canvas
			
			canvas.setCommandListener(this);
			canvas.addCommand(new Command("Quit", Command.EXIT, 1));
			canvas.addCommand(new Command("Search", Command.ITEM, 1));
			canvas.setFireListener(new FireListener(){
				public void fire() {
					navigate(drawer.getCenter());
				}});
			
			streetFinder = new StreetFinder(this);
			
			//waitSplashTime
			if(splashtime>0)
				Thread.sleep(splashtime-(System.currentTimeMillis()-time));
			
			//Start canvas
			display.setCurrent(canvas);
			canvas.start();
			
			//Load Publicity Layer
			String s = Properties.getProperty("publicity");
			if(s != null)
				loadWebLayer(s);
			
			//Load Services Layers
			s = Properties.getProperty("services");
			if(s != null){
				services =new ServicesMap(s);
				map.appendMap(services);
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	private void loadWebLayer(String s) {
		final MemMap publicity = new MemMap(0);
		try{
			publicity.cacheMap(new FileMap(HomeGetter.getInstance(),s));
		}catch(Exception ex){
			quit();
			canvas.pleaseWait();
			return;
		}
		map.appendMap(publicity);
	}

	private void loadSplash(Display display) {
		MemMap memSplashMap = new MemMap(10);
		FileMap fileSplashMap = new FileMap(ResourceGetter.getInstance(), Properties.getProperty("splashMap"));
		memSplashMap.cacheMap(fileSplashMap);
		Rectangle area = null;
		FigureIterator figs = memSplashMap.getFigures(null);
		while(figs.hasNext())
			if(area == null)
				area = new Rectangle(figs.next().getBoundingBox());
			else
				area.unite(figs.next().getBoundingBox());
		
		drawer  = new DefaultMapDrawer(memSplashMap);
		
		Canvas c = new Canvas(){
			ScreenGraphics sg = new ScreenGraphics();
			Rectangle clip = new Rectangle(0, 0, getWidth(), getHeight());
			protected void paint(Graphics g) {
				sg.setGraphics(g);
				drawer.drawFirstPass(sg, clip, transform);
				drawer.drawSecondPass(sg, clip, transform);
				drawer.drawVolatilePass(sg, clip, transform, 0);
			}
		};
		transform.setScreenCentre(c.getWidth(), c.getHeight());
		transform.adjust(area, c.getWidth(), c.getHeight());
		display.setCurrent(c);
		//CeluMap(r)
		//Celumap(r) uses minimal internet bandwidth to get sponsor information.
		//www.celumap.com.ar
	}

	protected void pauseApp() {
		// TODO Auto-generated method stub
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		notifyDestroyed();
	}

	public void commandAction(Command c, Displayable arg1) {
		if(c.getCommandType()==Command.EXIT)
			notifyDestroyed();
		if(c.getCommandType()==Command.ITEM){
			canvas.pleaseWait();
			display.setCurrent(streetFinder);
		}
	}

	public void highlight(Figure f) {
		if(f!=null){
			transform.adjust(f.getBoundingBox(), canvas.getWidth(), canvas.getHeight());
			drawer.setHighlight(f);
			canvas.invalidate();
		}
		canvas.pleaseResume();
		display.setCurrent(canvas);
	}
	
	public void navigate(Figure figure) {
		if(figure instanceof WebIcon){
			//canvas.pleaseWait();
			
		}
	}
	
	public void showMap(){
		display.setCurrent(canvas);
		canvas.invalidate();
		canvas.pleaseResume();
	}

	public void quit() {
		notifyDestroyed();
	}
	 
}
